<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <!-- 左侧导航 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" class="avatar" />
          用户名：
        </li>
        <li>{{ userInfo.nickName }}</li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isdlan">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeisShowlogin(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeixinsmAPI } from "../request/api";
export default {
  methods: {
    ...mapMutations({
      changeisShowlogin: "isshowmodal/changeisShowlogin",
      changeisdlan: "isdlan/changeisdlan",
      cchuserInfo: "user/cchuserInfo",
    }),
    ...mapActions({
      fzdsq: "istsk/fzdsq",
      apichangeuserInfo: "user/apichangeuserInfo",
    }),
  },
  computed: {
    ...mapState({
      isdlan: (state) => state.isdlan.isdlan,
      userInfo: (state) => state.user.userInfo,
      cartTotal: (state) => state.user.cartTotal,
    }),
  },
  created() {
    this.$router.onReady(async () => {
      let pathcode = this.$route.query.code;
      if (pathcode) {
        let res = await WeixinsmAPI({
          code: pathcode,
        });
        console.log(res);
        if (res.code == 0) {
          //登录成功
          this.fzdsq({
            isshow: "true",
            title: "微信登录成功",
            type: "zhengque",
          });
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.apichangeuserInfo();
          this.changeisdlan(true);
          this.$router.push(this.$route.path);
        } else if (res.code == 400) {
          //二维码过期或已被使用
          this.fzdsq({
            isshow: "true",
            title: "二维码以过期请重新扫码",
            type: "cuowu",
          });
          this.changeisShowlogin(true);
        } else if (res.code == 407) {
          //关联手机号
          this.fzdsq({
            isshow: "true",
            title: res.message,
            type: "cuowu",
          });
          localStorage.setItem("uuid", res.uuid);
          this.changeisShowlogin(true);
          this.$router.push(this.$route.path);
        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeisdlan(Boolean(token));
        if (token) {
          this.apichangeuserInfo();
        } else {
          this.cchuserInfo();
        }
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  line-height: 40px;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;

    .avatar {
      width: 23px;
      height: 22px;
      border-radius: 13px;
      vertical-align: middle;
    }
    .right {
      display: flex;
      text-align: center;
      li {
        margin-left: 20px;
      }
      .login-btn {
        width: 124px;
        background: #0a328e;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>